using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


/* OWED */
using OWED.Document;
using OWED.Network;
using OWED.Network.Messages;
using OWED.Network.Messages.Reply;
using OWED.Network.Messages.General;

/// <summary>
/// Apply proposal class
/// </summary>
public partial class ApplyProposal : System.Web.UI.Page {

	/// <summary>
	/// Authentication details
	/// </summary>
	String[] AuthDetails;

	/// <summary>
	/// Backend connection
	/// </summary>
	Connection conn;

	/// <summary>
	/// Approve a proposal
	/// </summary>
	ApproveProposal request;

	/// <summary>
	/// Reply of proposals
	/// </summary>
	PassFail reply;

	/// <summary>
	/// ErrorMessage for View proposal
	/// </summary>
	protected Literal pageOutput;

	/// <summary>
	/// Page load event, not using .NET controls, so this is the main call
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		pageOutput.Text = "";
		/* check if user is logged in */
		if (!IsLoggedIn.Check(Request.Cookies["owed"])) {
			Response.Redirect("/Login.aspx");
		} else {
			AuthDetails = IsLoggedIn.GetAuthentication(Request.Cookies["owed"]);
		}

		if (Request.Form["whichDocument"] == null || Request.Form["whichProposal"] == null) {
			pageOutput.Text = "<h2>Error:</h2>No document name supplied";
			return;
		}

		/* instantiate connection and login*/
		conn = null;
		WebLogin.C(out conn, AuthDetails[0], AuthDetails[1]);
	
		/* send proposal request */
		request = new ApproveProposal(new User(AuthDetails[0]), new DocName(Request.Form["whichDocument"]), new ProposalID(int.Parse(Request.Form["whichProposal"])));
		reply = (PassFail)MessageGet.GetFromXml(conn.Send(request.ToXml()));
		if (!reply.pass) pageOutput.Text = "<h2>Error</h2>Permission Denied";
		else pageOutput.Text = "<h2>Success</h2>";
	}
}
